/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.event.thuoc;

import gui.thuoc.ThuocThemNCC_Dialog;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import model.dao.thuoc.ThuocDAO;
import model.entity.Gia;
import model.entity.NhaCungCap;
import model.entity.Thuoc;
import org.hibernate.HibernateException;

/**
 *
 * @author nv_ca@live.com
 */
public class ThuocThemFull_evt {

    public static final void btnThemnccActionPerformed() {
        ThuocThemNCC_Dialog thuocThemNCC = new ThuocThemNCC_Dialog(null, true);
        thuocThemNCC.setLocationRelativeTo(null);
        thuocThemNCC.setVisible(true);
    }

    public static final void btnCloseActionPerformed(JDialog dlg) {
        dlg.dispose();
    }

    public static final void btnSaveActionPerformed(JTextField ten, JTextField giagoc, JTextField giaban, JTextField donvi, JTextField soluong, JComboBox ncc, JTextArea ghichu) {
        try {
            String thuocTen = ten.getText();
            int giaGoc = Integer.parseInt(giagoc.getText());
            int giaBan = Integer.parseInt(giaban.getText());
            String donVi = donvi.getText();
            Gia gia = new Gia(giaGoc, giaBan, donVi);
            int soLuong = Integer.parseInt(soluong.getText());
            NhaCungCap nhacungcap = (NhaCungCap) ncc.getSelectedItem();
            String ghiChu = ghichu.getText();
            Thuoc thuoc = new Thuoc(nhacungcap, gia, thuocTen, soLuong, ghiChu);
            ThuocDAO.insert(thuoc);
        } catch (NumberFormatException | HibernateException ex) {
            JOptionPane.showMessageDialog(null, "Thông tin không đúng.");
            return;
        }
        JOptionPane.showMessageDialog(null, "Thêm thuốc thành công.");
    }

}
